<script lang="ts" setup>
import { onMounted, ref} from 'vue';
import { useAppStore  }  from "@/stores"
import { formatTime } from "@/utils/index"
import type { postItem } from "@/types/userInfo.d.ts"
import { ImageAddHttp } from "@/utils/index"
import { showImagePreview } from 'vant';
// 接收父组件传递参数post
const appStore = useAppStore()

const Props = defineProps<{ post: postItem }>()
const emit = defineEmits(['vote']);
const imgLen = ref(0)
const timeStr = ref("")

// 引用文字容器元素
const contentRef = ref<HTMLElement | null>(null);
// 是否展开全文
const isExpanded = ref(false);
// 是否显示“全文”按钮
const showToggleButton = ref(false);
const videoRef = ref<HTMLVideoElement | null>(null);
const videoIsPlaying = ref(false)
const isFullscreen = ref(false);
// 播放视频方法
const playVideo = () => {
  appStore.setPreVideos(ImageAddHttp(Props.post.video_file))
  appStore.setShowVideoLayer(true)
};
onMounted(async () => {
  // 判断图片数量
  if(Props.post.images.length == 1){
    imgLen.value = 1
  }


  // 判断文字是否超出5行
  const el = contentRef.value;
  if (el) {
    const maxHeight = parseInt(getComputedStyle(el).lineHeight, 10) * 5; // 计算5行的高度
    if (el.scrollHeight > maxHeight) {
      showToggleButton.value = true; // 如果超出5行，显示“全文”按钮
    }
  }

  // 格式化时间
  timeStr.value = formatTime(Props.post.createtime)
  
});


const vote = async function(){
  emit('vote', Props.post.id)
}

// 切换“全文”状态
const toggleExpand = () => {
  isExpanded.value = !isExpanded.value;
};



function previewImage(index: number) {
    const images = Props.post.images.map((image) => ImageAddHttp(image));
    showImagePreview({
      images,
      startPosition: index,
    });
}


</script>
<template>
   <div class="post">
    <div class="avatar_box">
      <img class="avatar" :src="ImageAddHttp(Props.post.avatar)" alt="用户头像" />
    </div>
    <div class="content">
      <div class="name">{{Props.post.username}}</div>
      <div class="text"  :class="{ expanded: isExpanded }" ref="contentRef">{{Props.post.post}}</div>
      <button
        v-if="showToggleButton"
        class="toggle-button"
        @click="toggleExpand"
      >
        {{ isExpanded ? "收起" : "全文" }}
      </button>
      <!-- 1个  4个  其他 -->
      <!-- 视频 -->
      <div :class="imgLen != 0 ? 'images'+imgLen : 'images' ">
        <div v-if="Props.post.video_file" class="video_box">
          <img v-if="!videoIsPlaying" src="@/static/cover.png" class="play_icon" alt="">
          <video class="video" muted autoplay="true" :class="{ fullscreen: isFullscreen }" @click="playVideo" ref="videoRef" loadedmetadata="" :src="ImageAddHttp(Props.post.video_file)"  ></video>
        </div>
        <img class="image-item"  v-for="(image,index) in Props.post.images"  :key="index" :src="ImageAddHttp(image)"   @click="previewImage(index)" alt="">
      </div>
      <div class="footer">
        <div class="desc">{{timeStr}}</div>
        <div class="comment" @click="vote()">
          <img :src="Props.post.is_like ? './src/static/heart_icon_active.png' : './src/static/heart_icon.png'" alt="">
          <span>{{Props.post.voteNum}}</span>
        </div>
      </div>
    </div>
  </div>
</template>



<style lang="scss" scoped>
.post {
  padding: 16px;
  background: #fff;
  font-family: Arial, sans-serif;
  display: flex;
  // 横向排列
  flex-direction: row;
  .avatar_box{
    width: 54px;
    img{
      width: 54px;
      height: 54px;
      border-radius:4px
    }

  }
  .content {
    flex: 1;
    padding-left: 10px;
    .name {
      font-size: 1.2rem;
      font-weight: bold;
      color: #576B95 ;
    }
    .text {
      font-size: 1rem;
      color: #1A1A1A ;
      margin-top: 4px;
      line-height: 1.4;
      overflow: hidden;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 5; /* 限制最多显示5行 */
    }
    .text.expanded {
      -webkit-line-clamp: unset; /* 取消行数限制 */
      display: block; /* 恢复完整显示 */
      overflow: visible;
    }

    /* 全文/收起按钮样式 */
    .toggle-button {
      margin-top: 8px;
      font-size: 1.1rem;
      color: #576B95 ;
      background: none;
      border: none;
      cursor: pointer;
      padding: 0;
    }

    .images {
      display: grid;
      grid-template-columns: repeat(3, 1fr); /* 每行最多3张图片 */
      gap: 4px; /* 图片之间的间距 */
      width: 100%; /* 父容器宽度自适应 */
      .image-item {
        width: 100%;
        height: 100%;
        // padding-bottom: 100%; /* 保持图片为正方形 */
        object-fit: cover; /* 确保图片按比例填充 */
        border-radius: 2px; /* 可选，圆角 */
        cursor: pointer; /* 鼠标移上变手型 */
        // background-color: #f0f0f0; /* 添加背景色，方便查看 */
      }
      .video_box{
        height: auto;
        width: 90%;
        position: relative;
        float: left;
        .play_icon{
          width: 40px;
          height: auto;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
        video{
          max-height: 190px;
          width: 100%;
        }
        .video {
          width: 100%;
          height: 100%;
          object-fit: cover;
          cursor: pointer;
          transition: all 0.1s ease; /* 平滑缩放动画 */
        }
        /* 全屏样式 */
        .video.fullscreen {
          position: fixed;
          top: 0;
          left: 0;
          width: 100vw !important;
          height: 100vh !important;
          z-index: 1000;
          border-radius: 0;
          background: black;
          max-height:  100vh !important;
        }
      }
      .video_box::after{
        content: '';
        display: block;
        clear: both;
      }
    }
    .images1{
      margin-top: 10px;
      overflow: hidden;
      img{
        max-height: 190px;
        width: auto;
      }
      .video_box{
        height: auto;
        width: auto;
        position: relative;
        float: left;
        .play_icon{
          width: 40px;
          height: auto;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
        video{
          max-height: 190px;
          width: auto;
        }
        .video {
          width: 100%;
          height: 100%;
          object-fit: cover;
          cursor: pointer;
          transition: all 0.1s ease; /* 平滑缩放动画 */
        }
        /* 全屏样式 */
        .video.fullscreen {
          position: fixed;
          top: 0;
          left: 0;
          width: 100vw !important;
          height: 100vh !important;
          z-index: 1000;
          border-radius: 0;
          background: black;
          max-height:  100vh !important;
        }
      }
      .video_box::after{
        content: '';
        display: block;
        clear: both;
      }
     
    }
   
    .footer {
      margin-top: 10px;
      display: flex;
      justify-content: space-between;
      .desc {
        font-size: 0.8rem;
        color: #999;
      }
      .comment {
        display: flex;
        align-items: center;
        img {
          width: 16px;
          height: 16px;
        }
        span {
          font-size: 0.8rem;
          color: #666;
          margin-left: 5px;
        }
      }
    }
  }
}
</style>
